var rightSideView = function (root) {
    let result = []
    if (!root) return result;
    let queue = []
    queue.push(root);

    while (queue.length) {
        let len = queue.length

        for (let i = 0; i < len; i++) {
            let node = queue.shift()
            if (i === len - 1) {
                result.push(node.val)
            }
            if (node.left) {
                queue.push(node.left)
            }
            if (node.right) {
                queue.push(node.right)
            }
        }
    }
    return result;
};